package com.lz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lz.pojo.SysUser;
import com.lz.security.pojo.LoginUser;
import com.lz.security.utils.SecurityUtil;
import com.lz.service.SysUserService;
import com.lz.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    /** 分页查询所有数据 */
//    @PreAuthorize("hasAnyAuthority('sys:user:query')")
    @GetMapping("/page")
    public Result page(@RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                       SysUser sysUser){
        return Result.success(sysUserService.pageVo(pageNumber,pageSize,sysUser));
    }

    /** 查询所有数据 */
    @GetMapping("/list")
//    @PreAuthorize("hasAnyAuthority('sys:user:query')")
    public Result list(){
        return Result.success(sysUserService.list());
    }

    /** 通过主键查询单条数据 */
    @GetMapping("/get/{id}")
//    @PreAuthorize("hasAnyAuthority('sys:user:query')")
    public Result get(@PathVariable("id") String id){
        return Result.success(sysUserService.get(id));
    }

    /** 通过用户名查询用户 */
//    @PreAuthorize("hasAnyAuthority('sys:user:query')")
    @GetMapping("/getByUsername/{username}")
    public Result getByUsername(@PathVariable("username") String username){
        return Result.success(sysUserService.getOne(new QueryWrapper<SysUser>().eq("username",username)));
    }

    /** 新增数据 */
    @PostMapping("/save")
//    @PreAuthorize("hasAnyAuthority('sys:user:save')")
    public Result save(@RequestBody SysUser sysUser){
        return Result.success(sysUserService.add(sysUser));
    }

    /** 编辑数据 */
//    @PreAuthorize("hasAnyAuthority('sys:user:update')")
    @PostMapping("/edit")
    public Result edit(@RequestBody SysUser sysUser){
        return Result.success(sysUserService.edit(sysUser));
    }

    /** 删除单条数据 */
//    @PreAuthorize("hasAnyAuthority('sys:user:del')")
    @GetMapping("/removeOne")
    public Result delete(@RequestParam("id") String id){
        System.out.println("***********"+id);
        return Result.success(sysUserService.editUser(id));
    }

    /** 批量删除数据 */
    @GetMapping("/removeAll")
//    @PreAuthorize("hasAnyAuthority('sys:user:del')")
    public Result deleteBatch(@RequestParam("idList") List<Integer> idList){
        return Result.success(sysUserService.removeBatchByIds(idList));
    }

}













